<?php
/**
 * Simple Light PHP Framework
 *
 * @copyright Marc PEPIN 2012
 * @author Marc PEPIN
 *
 */

defined('SLP_ROOT') or die("You've not access to this page");
 
/** 
* Simple 		Light Php Framework File Class
* @package		Slp
* @author    	Marc PEPIN
*/ 
class Slp_File { 
	/**
	*  file descriptor
	* @var ressource
	*/
	
	private $fd;
	
	
	/**
	* file name
	* @var string
	*/
	
	private $filename;
	
	
	/**
	* file status
	* @var bool
	*/
	
	private $is_open = false;
	
	
	/**
	* Constructor
	* @param string
	* @param bool
	*/
	
	public function __construct($filename, $create = false) {
		$this->filename = $filename;
		if ($create == false)
			$this->open($filename);
		else
			$this->create($filename);
	}
	
	
	/**
	* open file
	* @param filename 
	*/
	
     public function open($filename) {
		 $this->is_open = (!$this->fd = @fopen($filename, "cr+")) ? false : true;
	 }
	 
	 
	 /**
	 * open and truc file
	 * @param string
	 */
	 
	 public function openTrunc($filename) {
		 $this->is_open = (!$this->fd = @fopen($filename, "w")) ? false : true;
	 }
	 
	 
	 /**
	 * Check if file is open
	 *	@return true if file is open or false
	 */
	 
	 public function isOpen() {
		return $this->is_open; 
	 }
	 
	 /**
	 * Create file
	 * @param $filename filename
	 */
	 
	 public function create($filename) {
		 $this->is_open = (!$this->fd = @fopen($filename, "cw+")) ? false : true;
	 }
	 
	 /**
	 * get file information
	 * @ret object contain file info
	 */
	 
	 public function info() {
		$info = new stdClass;
		$info->opened = $this->is_open;
		if ($this->is_open) {
			$info->access = @fileatime($this->filename);
			$info->group = @filegroup($this->filename);
			$info->owned = @fileowner($this->filename);
			$info->perms = @fileperms($this->filename);
			$info->size = @filesize($this->filename);
			$info->type = @filetype($this->filename);
		}
		return $info;
	 }
	 
	 
	 /**
	 * move file cursor
	 * @ret bool
	 * @param int
	 * @param enum
	 */
	 
	 public function setPos($offset, $whence = SEEK_CUR) {
		 if ($this->is_open) 
			return @fseek($this->fd, $offset, $whence) ? true : false; 
		 return false;
	 }
	 
	 
	 /**
	 * get file cursor position
	 * @ret bool
	 */
	 
	 public function getPos() {
		 return $this->is_open ? ftell($this->fd) : 0;
	 }
	 
	 
	 /**
	 * append data 
	 * @param data to write
	 * @ret bool
	 */
	 
	 public function append($data) {
		$this->setPos(0, SEEK_END);
		return $this->write($data);
	 }
	 
	 
	 /**
	 * write data 
	 * @param data to write
	 * @ret bool
	 */
	 
	 public function write($data) {
		 if ($this->is_open)
			return @fwrite($this->fd, $data) ? true : false; 
		return false;
	 }
	
	 
	 /**
	 * Read content of file
	 */
	 
	 public function readAll() {
		 if ($this->is_open)
		 	return @file_get_contents($this->filename);
		return NULL;
	 }
	 
	 
	/**
	* close file descriptor 
	*/
	 
	public function close() {
		if ($this->fd)
			@fclose($this->fd);	
	}
	
	 
	/**
	* Clear file 
	*/
	
	public function clear() {
		$this->openTrunc($this->filename);	
	}
	/**
	* Destructor
	*/
	
	public function __destruct() {
		$this->close();	
	}
	
} 
?>